#include "String.h"

namespace bit{
	void test_string1()
	{
		bit::string s1("hello world");
		cout << s1.c_str() << endl;

		bit::string s2;
		cout << s2.c_str() << endl;

		for (size_t i = 0; i < s1.size(); i++)
		{
			s1[i]++;
			cout << s1[i] << " ";
		}
		cout << endl;
		
		//封装：屏蔽了底层实现细节，提供了一种简单通用访问容器的方式
		string::iterator it1 = s1.begin();
		while (it1 != s1.end())
		{
			cout << *it1 << " ";
			++it1;
		}
		cout << endl;

		//范围for底层也是迭代器
		for (auto e : s1)
		{
			cout << e << " ";
		}
		cout << endl;
		
		const bit::string s3("XXXXXX");
		string::const_iterator it3 = s3.begin();
		while (it3 != s3.end())
		{
			cout << *it3 << " ";
			++it3;
		}
		cout << endl;
		for (size_t i = 0; i < s3.size(); i++)
		{
			//s3[i]++; //不行
			cout << s3[i] << " ";
		}
		cout << endl;
	}


	void test_string2()
	{
		bit::string s1("hello world");
		cout << s1.c_str() << endl;

		s1.push_back('x');
		cout << s1.c_str() << endl;

		s1.append("yyyy");
		cout << s1.c_str() << endl;

		s1 += 'z';
		cout << s1.c_str() << endl;

		s1 += "后悔";
		cout << s1.c_str() << endl;
	}


	void test_string3()
	{
		bit::string s1("hello world");

		s1.insert(5, 'x');
		cout << s1.c_str() << endl;

		s1.insert(0, 'x');
		cout << s1.c_str() << endl;

		bit::string s2("hello world");
		s2.insert(6, "yyy");
		cout << s2.c_str() << endl;

		s2.insert(0, "yyy");
		cout << s2.c_str() << endl;

		bit::string s3("hello world");
		cout << s2.c_str() << endl;

		s3.erase(6, 10);
		cout << s2.c_str() << endl;
	}

	void test_string4()
	{
		bit::string s1("hello world");
		cout << s1.find('o') << endl;
		cout << s1.find("wor") << endl;
	}

	void test_string5()
	{
		bit::string s1("hello world");
		bit::string s2(s1); //深拷贝
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

		bit::string s3("y");
		s1 = s3;
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;

		bit::string s4("爱意随风起，风之意难平");
		s1 = s4;
		cout << s1.c_str() << endl;
		cout << s4.c_str() << endl;

		s1 = s1;
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;

		std::swap(s1, s3);
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;

		s1.swap(s3);
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;
	}
	void test_string6()
	{
		bit::string s1("hello world");
		bit::string s2 = s1.substr(0, 3);
		cout << s2.c_str() << endl;
	}

	void test_string7()
	{
		bit::string s1("hello world");
		bit::string s2("z1");
		if (s1 < s2) cout << "s1 < s2" << endl;
	}

	void test_string8()
	{
		bit::string s1("hello world");
		cout << s1 << endl;

		cin >> s1;
		//std::cin >> s1;
		cout << s1 << endl;
	}

	void test_string9()
	{
		bit::string s1("hello world");
		bit::string s2(s1);
		cout << (void*)s1.c_str() << endl;
		cout << (void*)s2.c_str() << endl;
	}
}


int main()
{
	bit::test_string9();
	//cout << typeid(std::string::iterator).name() << endl;
	//cout << typeid(bit::string::iterator).name() << endl;

	return 0;
}


